package cn.com.infcn.monitor.task.jvm;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

import org.apache.log4j.Logger;

import cn.com.infcn.monitor.bean.IFCJVMOperatingSystem;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;

/**
 * 用于操作系统的管理接口，Java 虚拟机在此操作系统上运行。
 * 
 * @author jijs
 *
 */
public class JVMOperatingSystemTask implements BaseTask {

	private static final Logger LOGGER = Logger.getLogger(JVMOperatingSystemTask.class);

	@Override
	public IFCJVMOperatingSystem execute() throws Exception {
		return jvmOperatingSystem();
	}

	public IFCJVMOperatingSystem jvmOperatingSystem() {
		IFCJVMOperatingSystem ifcOperatingSystem = null;

		try {

			OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
			ifcOperatingSystem = new IFCJVMOperatingSystem();

			IFCBeanUtils.copyProperties(operatingSystemMXBean, OperatingSystemMXBean.class, ifcOperatingSystem);

			return ifcOperatingSystem;
		} catch (Exception e) {
			LOGGER.warn(e.getMessage(), e);
		}

		return null;
	}
	
	/*private void copyProperties(OperatingSystemMXBean operatingSystemMXBean, IFCJVMOperatingSystem ifcOperatingSystem){
		ifcOperatingSystem.setArch(operatingSystemMXBean.getArch());
		ifcOperatingSystem.setAvailableProcessors(operatingSystemMXBean.getAvailableProcessors());
		ifcOperatingSystem.setName(operatingSystemMXBean.getName());
		ifcOperatingSystem.setSystemLoadAverage(operatingSystemMXBean.getSystemLoadAverage());
		ifcOperatingSystem.setVersion(operatingSystemMXBean.getVersion());
	}*/

	public static void main(String[] args) {
		JVMOperatingSystemTask task = new JVMOperatingSystemTask();
		IFCJVMOperatingSystem ifcOperatingSystem = task.jvmOperatingSystem();

		System.out.println(ifcOperatingSystem.getAvailableProcessors());
		System.out.println(ifcOperatingSystem.getSystemLoadAverage());
		System.out.println(ifcOperatingSystem.getName());
		System.out.println(ifcOperatingSystem.getArch());
		System.out.println(ifcOperatingSystem.getVersion());

	}

}
